﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Decision.IService;
using FantastPlatform.Framework;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Framework.Core;

namespace FantastPlatform.Decision.UI.Service
{
    public partial class DutyTelService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IDutyTelService DtService = ServiceFactory.CreateService<IDutyTelService>();
            IDutyTel Tellist = new FantastPlatform.Decision.Model.DutyTel();
            ConditionGroup group = new ConditionGroup();
            Condition enableCondition = new Condition() { Field = "Enable", Type = typeof(bool).FullName, Value1 = true };
            group.Conditions.Add(enableCondition);
            switch (Action)
            {
                case "Add":
                    string data = Data;
                    DutyTelListView view = (DutyTelListView)JsonToEntity<DutyTelListView>(RepairString(data));
                    if (view != null)
                    {
                        Tellist.Id = view.Id;
                        Tellist.Station = view.Station;
                        Tellist.Office = view.Office;
                        Tellist.Phone = view.Phone;
                        Tellist.Phone1 = view.Phone1;
                        Tellist.Home = view.Home;
                    }
                    if (Tellist.Id == 0)
                    {
                        DtService.Create(Tellist);
                        ReturnSuccessMessage("保存成功");
                    }
                    else
                    {
                        IDutyTel tellist1 = DtService.Get(view.Id);
                        tellist1.Station = view.Station;
                        tellist1.Office = view.Office;
                        tellist1.Phone = view.Phone;
                        tellist1.Phone1 = view.Phone1;
                        tellist1.Home = view.Home;
                        tellist1.ModifyDate = System.DateTime.Now;
                        DtService.Update(tellist1);
                        ReturnSuccessMessage("更新成功");
                    }
                    break;
                case "Search":
                    int total;
                    object[] dl;
                    group.Conditions.Add(enableCondition);
                    IList<IDutyTel> DtList = DtService.Search(group, null, PageSize, SelfPage, out total);
                    dl = (from m in DtList
                          select new
                          {
                              Id = m.Id,
                              Station = m.Station,
                              Office = m.Office,
                              Phone = m.Phone,
                              Phone1 = m.Phone1,
                              Home = m.Home
                          }).OrderByDescending(m => m.Id).ToArray();
                    ReturnList(total, dl);
                    break;
                case "Get":
                    Tellist = DtService.Get(Id);
                    object Dtobj = new
                    {
                        Id = Tellist.Id,
                        Station = Tellist.Station,
                        Office = Tellist.Office,
                        Phone = Tellist.Phone,
                        Phone1 = Tellist.Phone1,
                        Home = Tellist.Home
                    };
                    ReturnObj(Dtobj);
                    break;
                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    DtService.Delete(idList);
                    ReturnSuccessMessage("删除成功");
                    break;
                case "SearchByCondition":
                    view = (DutyTelListView)JsonToEntity<DutyTelListView>(RepairString(Data));
                    int searchTotal = 0;
                    if (!string.IsNullOrEmpty(view.Station))
                    {
                        Condition dltsearch = new Condition() { Field = "Station", Type = typeof(string).FullName, Value1 = "%" + view.Station + "%", Comparison = Comparisons.Like, Relation = Relations.And };
                        group.Conditions.Add(dltsearch);
                    }
                    DtList = DtService.Search(group, null, PageSize, SelfPage, out searchTotal);
                    dl = (from m in DtList
                          select new
                          {
                              Id = m.Id,
                              Station = m.Station,
                              Office = m.Office,
                              Phone = m.Phone,
                              Phone1 = m.Phone1,
                              Home = m.Home,
                              Name = m.Name,
                          }).OrderByDescending(m => m.Id).ToArray();
                    ReturnList(searchTotal, dl);
                    break;

            }
        }
        public class DutyTelListView
        {
            public int Id { get; set; }
            public string Station { get; set; }
            public string Office { get; set; }
            public string Phone { get; set; }
            public string Phone1 { get; set; }
            public string Home { get; set; }
        }
    }
}